//
//  ViewController3.m
//  SaveDate1
//
//  Created by Justsoso on 16/3/14.
//  Copyright © 2016年 Justsoso. All rights reserved.
//


#import "ViewController3.h"

#import "Student.h"
@interface ViewController3 ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *stuList;
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController3

- (NSMutableArray *)stuList
{
    if (_stuList == nil)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self dataPath]])
        {
            NSData *data = [NSData dataWithContentsOfFile:[self dataPath]];
            NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            _stuList = [unArch decodeObjectForKey:@"Students"];
            [unArch finishDecoding];
        }
        else
        {
            _stuList = [NSMutableArray array];
        }
    }
    return _stuList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


/** plist文件储存位置*/
- (NSString *)dataPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"students.arch"];
    return filePath;
}

/** 保存数据到plist(覆盖以前的数据)*/
- (void)saveData
{
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [arch encodeObject:self.stuList forKey:@"Students"];
    [arch finishEncoding];
    [mData writeToFile:[self dataPath] atomically:YES];
}
- (IBAction)add:(id)sender
{
    Student *m = [[Student alloc] initWithId:self.idTextField.text
                                     andName:self.nameTextField.text
                                      andAge:self.ageTextField.text];
    [self.stuList addObject:m];
    [self saveData];
    [self.tableView reloadData];
}
- (IBAction)remove:(id)sender
{
    for (Student *m in self.stuList)
    {
        if ([m.stuId isEqualToString:self.idTextField.text])
        {
            [self.stuList removeObject:m];
            break;
        }
    }
    [self saveData];
    [self.tableView reloadData];
}
- (IBAction)modify:(id)sender
{
    for (Student *m in self.stuList)
    {
        if ([m.stuId isEqualToString:self.idTextField.text])
        {
            m.stuName = self.nameTextField.text;
            m.stuAge = self.ageTextField.text;
            break;
        }
    }
    [self saveData];
    [self.tableView reloadData];
}
- (IBAction)find:(id)sender
{
    for (Student *m in self.stuList)
    {
        if ([m.stuId isEqualToString:self.idTextField.text])
        {
            self.nameTextField.text = m.stuName;
            self.ageTextField.text = m.stuAge;
            break;
        }
    }
    [self saveData];
    [self.tableView reloadData];
}
- (IBAction)findAll:(id)sender
{
    for (Student *m in self.stuList)
    {
        NSLog(@"%@",m);
    }
}


#pragma mark - 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stuList.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    UILabel *label1 = [cell.contentView viewWithTag:104];
    UILabel *label2 = [cell.contentView viewWithTag:105];
    UILabel *label3 = [cell.contentView viewWithTag:106];
    
    Student *stu = self.stuList[indexPath.row];
    label1.text = stu.stuId;
    label2.text = stu.stuName;
    label3.text = stu.stuAge;
    
    return cell;
}
@end
